<?php
namespace Home\Controller;
use Home\Controller\CommonController;
use Think\Page;

class IndexController extends CommonController{
//    登陆界面
        public function index(){  
            if(session('?user_id') && session('?user_name')){
               $this->display('home'); 
            }
               $this->display();
            }
            
        public function logout(){
            session(null);
            $this->display('index');
         }

       public function home(){
             $this->display('home');
        }    
        
        public function top(){
            $this->assign('name', session('user_name'));
            $this->display();
        }

        public function menu(){
            $data['user_id'] = session('user_id');           // dump($data);
            $admin_user=D('AdminUser');
            $role_id = $admin_user->find('admin_user',$data,'role_id');           // dump($role_id);
            $rules = $admin_user->find('auth_group',['id'=> intval($role_id['role_id'])],'rules');           // dump($rules);
            $r = explode(',', $rules['rules']);           // dump($r);
            $condition['id'] = ['in',$r];
            $result = $admin_user->select('auth_rule',$condition);           // dump($result);
            $this->assign('auth',$result);
            $this->display();
        }

        public function main(){
            $this->display();
        }
       
        public function add(){
            $user_name = I('user_name','','trim');
            $data = '';
            if(!empty($user_name)){
                $data['user_name'] = ['like',"%".$user_name."%"];
            }
            $data['status'] = 1;
            $admin_user = D('AdminUser');
            $totalRows = $admin_user->countAll('admin_user',$data);//dump($totalRows);exit;
            $page = new Page($totalRows,15);
            $result = $admin_user->select('admin_user',$data,'','user_id desc',$page->firstRow,$page->listRows);          //  dump($result);
            $this->assign('page',$page->show());
            $this->assign('userInfo',$result);
            $this->display();
        }
        
        public function addUser(){
            $user_name = I('user_name','','trim');
            $password = I('password','','trim');
            $mark = I('mark');
//            dump(I());die;
            if(!empty($user_name) && !empty($password)){
                $admin_user = D('AdminUser');
                 $data['user_name'] = $user_name;
                $userName = $admin_user->find('admin_user',$data);
                if(!empty($userName)){
                    $this->error('该用户已存在！');
                }
                $data['ec_salt'] = '3241';
                $data['password'] = md5(md5($password).$data['ec_salt']);
                $data['add_time'] = time();
                $data['last_login'] = time();
                $data['status'] = 1;
                $data['role_id'] = empty($mark) ? 2 : 3;
//                dump($data);die;
               
                $userInfo = $admin_user->insertOne('admin_user',$data);
                if($userInfo){
                    $this->error('添加成功');
                }         
            }
            $this->error('添加失败');
        }
        
        public function editPassword(){
            $user_id = I('user_id','','intval');
            $password = I('editPassword','','trim');
            if(!empty($user_id) && !empty($password)){
                $admin_user = D('AdminUser');
                $password = md5(md5($password).'3241');
                $result = $admin_user->update('admin_user',['user_id'=>$user_id],'password',$password);
                if($result != FALSE){
                    $this->error('修改成功!');
                }
                $this->error('修改失败!');
            }
            $this->error('修改数据缺失!');
        }
        
        public function delUser(){
            $user_id = I('user_id','','intval');
            if($user_id == 1){
                $this->ajaxReturn(['status'=>0,'msg'=>'管理员用户无法删除!']);
            }else{
                $admin_user =D('AdminUser');
                $update_status = $admin_user->change('admin_user',['user_id'=>$user_id],['status'=>0]);
                if($update_status){
                    $this->ajaxReturn(['status'=>1,'msg'=>'删除成功!']);
                }
                $this->ajaxReturn(['status'=>1,'msg'=>'删除失败!']);
            }
        }

        public function insert(){
            $this->display();
        }
        
        public function export(){
            $this->assign('user_id',session('user_id'));
            $this->display();
        }
        
        public function order(){
            $user_name = I('user_name','','trim');
            $order_sn = I('order_sn','','trim');
            $order_status = I('order_status','','intval');          // dump(gettype($order_status));
            $where = 1;
            if($user_name == '' && $order_sn != ''){             
                $where = "order_sn =".$order_sn;
            }elseif($user_name != '' && $order_sn == ''){
                $where = "user_name like '%".$user_name."%'";
            }elseif($user_name != '' && $order_sn != ''){
                $where = "order_sn =".$order_sn." or user_name like '%".$user_name."%'";
            }elseif(gettype($order_status) == 'integer'){
                $where = "order_status = ".$order_status;
            }else{
                $where = '';
            }
            $admin_user = D('AdminUser');
            $totalRows = $admin_user->totals();//dump($totalRows);
            $page = new Page(intval($totalRows[0]['count(*)']),1);
            $result = $admin_user->showOrder($where,$page->firstRow,$page->listRows);
            $this->assign('page',$page->show());
            $this->assign('orderInfo',$result);
            $this->display();
        }
        
        public function userOrder(){
            $user_id = I('user_id',''.'intval');
            if(!empty($user_id)){
                $data['user_id'] = $user_id;
                $admin_user = D('AdminUser');
                $totalRows = $admin_user->countAll('order_info',$data);//dump($totalRows);
                $page = new Page($totalRows,15);
                $result = $admin_user->select('order_info',$data,'','addtime desc',$page->firstRow,$page->listRows);          // dump($result);
                $this->assign('page',$page->show());
                $this->assign('orderInfo',$result);
                $this->display('userOrder');
            }
        }
        
        public function orderDetial(){
            $order_id = I('order_id','','intval');
            $admin_user = D('AdminUser');
            $record = $admin_user->select('record',['order_id'=>$order_id]);
            $this->assign('records',$record);
            $this->display('orderDetial');
        }

        public function searchUserName(){
            $user_name = I('user_name'); 
            $admin_user = D('AdminUser');
            $result = $admin_user->find('admin_user',['user_name'=>$user_name]);
            if(!empty($result)){
                 $this->ajaxReturn(['status'=>0,'msg'=>'该用户已存在!']);
            }
            $this->ajaxReturn(['status'=>1,'msg'=>'可以注册!']);
        }


        public function searchId(){
            $idname = I('idname','','trim');
            $idcard = I('idcard','','trim');
            if($idname == '' && $idcard != ''){             
                $map['idcard'] = array('EQ',$idcard);            
            }elseif($idname != '' && $idcard == ''){
                $map['idname'] = array('EQ',$idname);
            }else{
                $map['idname'] = array('EQ',$idname);
                $map['idcard'] = array('EQ',$idcard);
                $map['_logic'] = 'OR';
            }
            
            $admin_user = D('AdminUser');
            $totalRows = $admin_user->countAll('identity_information',$map);           // dump($totalRows);
            $page = new Page($totalRows,5);
             $result = $admin_user->select('identity_information',$map,'','id desc',$page->firstRow,$page->listRows);             //dump($result);
             $user_id = session('user_id');
             $user_name = session('user_name');
             
                if(!empty($result)){                  
                    $order_goods = [];
                    foreach ($result as $key=>$value) {
                        if($value['isupload'] == 1){
                            $result[$key]['idphoto'] = substr($value['idcard'],0,2).'/'.$value['idcard'].'.jpg';
                        }
                         if($user_id != 1 && $user_name != 'admin'){              
                            $recordResult = $admin_user->find('record',['user_id'=>$user_id,'idcard'=>$value['idcard']]);                       
                            if(empty($recordResult)){
                                $record_id = $admin_user->insertOne('record',['user_id'=>$user_id,'idname'=>$value['idname'],'idcard'=>$value['idcard'],'num'=>1]);
                                $order_goods[] = $record_id;
                            }else{
                                $admin_user->plus('record',['id'=>$recordResult['id']],'num',1);
                            }
                       }
                    }    
                    
                    if($user_id != 1 && $user_name != 'admin'){
                        if(!empty($order_goods)){
                            $order_sn = $this->get_order_sn();
                            $total_price = count($order_goods)*3;
                            $order_id = $admin_user->insertOne('order_info',['user_id'=>$user_id,'user_name'=>$user_name,'order_sn'=>$order_sn,'order_status'=>0,'total_price'=>$total_price,'addtime'=> time()]);
                            foreach ($order_goods as $id) {
                                $admin_user->update('record',['id'=>$id],'order_id',$order_id);
                            }
                     }
                    }
                }
       
              $this->assign('page',$page->show());
              $this->assign('userInfo',$result);
              $this->display('export');
        }
        
        public function checkStatus(){
            $order_id = I('order_id','','intval');
            $admin_user = D('AdminUser');
            $result = $admin_user->update('order_info',['order_id'=>$order_id],'order_status',1);
            if($result){
               $this->ajaxReturn(['status'=>1]); 
            }
            $this->ajaxReturn(['status'=>0]); 
        }


//        生成验证码
        public function captcha(){
            $Verify = new \Think\Verify(['fontSize'=>40,'codeSet'=>'0123456789']);
            $Verify->entry();
        }
        
//        验证验证码
        public function check($code){
             $verify = new \Think\Verify();
             return $verify->check($code, $id);
        }
//        登陆判断
        public function login(){
            $captcha = I('captcha');
            if($this->check($captcha)){//关闭验证码
                 $AdminUser = D('AdminUser');   
                 $data['user_name'] = I('name');
                 $data['status'] = 1;
                 $field='ec_salt';
                 $ec_salt = $AdminUser->find('admin_user',$data,'ec_salt');
                 if(!empty($ec_salt)){
                     $pwd= md5(md5(I('pwd')).$ec_salt['ec_salt']);
                     $data['password']=$pwd;
                     $user=$AdminUser->find('admin_user',$data);    
                     if(!empty($user)){
                         $expire = 3600;
                         if(I('checked','','intval') == 1){
                             $expire = 3600*24*3;
                         }
                         session(['expire'=>$expire]);
                         session('user_name',$user['user_name']);
                         session('user_id',$user['user_id']);
                         $this->ajaxReturn(['status'=>1,'url'=>U('index/home')]);
                     }else{
                         $this->ajaxReturn(['status'=>0,'msg'=>'密码错误!']);
                     }
                 }else{
                      $this->ajaxReturn(['status'=>0,'msg'=>'该用户不存在!']);
                 }
            }else{
                 $this->ajaxReturn(['status'=>0,'msg'=>'验证码错误!']);
            }
            
        }

        public function download(){
                $type = I('type');
               if($type == 'zip'){
                   $filename=realpath("./Uploads/download/idinfo.zip");
                   $filename2 = 'idinfo.zip';                
               }elseif ($type == 'arrange') {
                   $filename=realpath("./Uploads/download/arrange.zip");
                   $filename2 = 'arrange.zip';
               }elseif ($type == 'three') {
                   $filename=realpath("./Uploads/download/three.xlsx");
                   $filename2 = 'three.xlsx';
               }else{
                    $filename=realpath("./Uploads/download/info.xlsx");
                    $filename2 = 'info.xlsx';
               }
                Header( "Content-type:  application/octet-stream "); 
                Header( "Accept-Ranges:  bytes "); 
                Header( "Accept-Length: " .filesize($filename));
                header( "Content-Disposition:  attachment;  filename=".$filename2); 
                readfile($filename); 
        }   
}